var app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    buthiddenNext: false,
    buthiddenSubmit: false,
    nextShow: true,
    telephone: '',
    telephoneT: '',
    code: '',
    verifyCodeTime: '发送验证码',
    timer: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      telephone: options.telephone,
      telephoneT: options.telephone
    })
  },

  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this
    that.setData({
      telephone: e.detail.value
    })
    if (e.detail.value.length == 11) {
      that.setData({ buthiddenNext: false, buthiddenSubmit: false })
    } else {
      that.setData({ buthiddenNext: true, buthiddenSubmit: true })
    }
  },
  //手机号验证码前端校验
  onCodeEvent: function(e) {
    var that = this
    that.setData({
      code: e.detail.value.replace(/\D/g, '')
    })
  },

  //提交手机号 验证码
  onPhoneNext: function() {
    var that = this
    var telephone = that.data.telephone
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    var c = 60
    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
      return false
    } else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false
    } else {
      that.setData({
        buthiddenNext: false
      })
      that.data.timer = setInterval(function() {
        c = c - 1
        that.setData({
          verifyCodeTime: c + 's后重发',
          buthiddenNext: true
        })
        if (c == 0) {
          clearInterval(that.data.timer)
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          })
        }
      }, 1000)
      wx.request({
        method: 'POST',
        url: app.preUrl + 'obtainMessagePhone',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          telephone: that.data.telephone
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 100) {
            console.log('验证码发送成功')
          } else {
            wx.showToast({
              title: '服务器异常',
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
    }
  },

  onPhoneSubmit: function() {
    var that = this
    var telephone = that.data.telephone
    var code = that.data.code
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/

    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false
    } else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1000
      })
      return false
    } else if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1000
      })
      return false
    } else if (code == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
      return false
    } else if (code.length != 4) {
      wx.showToast({
        title: '验证码长度有误！',
        icon: 'none',
        duration: 1000
      })
      return false
    } else {
      that.setData({
        buthiddenSubmit: false
      })
      wx.showLoading({ title: '提交中' })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'updateUserPhone',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          telephone: telephone,
          code: code,
          id: app.globalData.userId
        },
        success: function(res) {
          wx.hideLoading()
          console.log(res)
          if (res.data.code == 100) {
            that.setData({
              buthiddenSubmit: true
            })
            wx.showToast({
              title: '手机号修改成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
    }
  },
  onUnload() {
    //页面卸载  当redirectTo或navigateBack的时候调用
    clearInterval(this.data.timer)
  }
})
